<?php

/**
 * 会话类
 * 
 * @package    01CMS
 * @subpackage system
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class Session
{

	/**
	 * 写入Cookie
	 * 
	 * @param 	string $key
	 * @param 	string $value
	 * @param 	int $keepTime 	有效期（秒）
	 * @param 	string $path
	 * @return 	bool
	 * @access 	public
	 */
	public static function putCookie ($key, $value, $keepTime = 300, $path = "/")
	{
		$status1 = setcookie($key, $value, time() + $keepTime, $path);
		$status2 = setcookie($key . '_c', encrypt($value), time() + $keepTime, $path);
		return ($status1 && $status2);
	}

	/**
	 * 删除COOKIE
	 * 
	 * @param 	string $key
	 * @return 	bool
	 * @access 	public
	 */
	public static function dropCookie ($key)
	{
		$status1 = setcookie($key, '', time() - 360000, "/");
		$status2 = setcookie($key . '_c', '', time() - 360000, "/");
		return ($status1 && $status2);
	}

	/**
	 * 获取Cookie值
	 * 
	 * @param 	string $key
	 * @return 	mix
	 * @access 	public
	 */
	public static function getCookie ($key)
	{
		if (! isset($_COOKIE[$key]) || ! isset($_COOKIE[$key . '_c']))
		{
			return;
		}
		
		if ($_COOKIE[$key . '_c'] != encrypt($_COOKIE[$key]))
		{
			return;
		}
		
		return $_COOKIE[$key];
	}

	/**
	 * 写入加密Cookie（设置验证码）
	 * 
	 * @param 	string 	$key
	 * @param 	string 	$value
	 * @param 	int 	$keepTime 有效期（秒）
	 * @param 	string 	$path
	 * @return 	bool
	 * @access 	public
	 */
	public static function setCode ($key, $value, $keepTime = 300, $path = "/")
	{
		return setcookie($key, encrypt(strtolower($value)), time() + $keepTime, $path);
	}

	/**
	 * 检测Cookie值（检测验证码）
	 * 
	 * @param 	string 	$key
	 * @param 	string 	$value
	 * @return 	bool
	 * @access 	public
	 */
	public static function checkCode ($key, $value)
	{
		if (! isset($_COOKIE[$key]))
		{
			return FALSE;
		}
		
		$getedValue = $_COOKIE[$key];
		setcookie($key, '', time() - 360000, "/");
		return (encrypt(strtolower($value)) == $getedValue) ? true : false;
	}
}